package com.prs.crm.dao.pm.impl;

import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.dao.pm.PmDocDao;
import com.prs.crm.domain.pm.PmDoc;
import com.prs.crm.domain.pm.PmProject;
import com.prs.crm.domain.pm.PmStage;

public class PmDocDaoImpl extends BaseDaoImpl<PmDoc> implements PmDocDao{

	@Override
	@SuppressWarnings("unchecked")
	public Collection<PmDoc> getPmDoc(final PmProject project, final PmStage stage) {
		// TODO Auto-generated method stub
		return (Collection<PmDoc>) this.getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(PmDoc.class);
				criteria.add(Restrictions.eq("project", project));
				criteria.add(Restrictions.eq("stage", stage));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<PmDoc> getPmDoc(final PmProject project) {
		// TODO Auto-generated method stub
		return (Collection<PmDoc>) this.getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(PmDoc.class);
				criteria.add(Restrictions.eq("project", project));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	@SuppressWarnings("unchecked")
	public Collection<PmDoc> getProjectCreateDoc(final PmProject project) {
		// TODO Auto-generated method stub
		return (Collection<PmDoc>) this.getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(PmDoc.class);
				criteria.add(Restrictions.eq("project", project));
				criteria.add(Restrictions.isNull("stage"));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

}
